package com.common.sdk.net.connect.http.util;

import android.util.Pair;

import com.android.sohu.sdk.common.toolbox.StringUtils;
import com.common.sdk.net.connect.http.center.tools.HttpLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.CacheControl;
import okhttp3.Request;

/**
 * Created by chengjiangsang116630 on 2018/2/26.
 */

public class OkhttpCacheUtil {

    /**
     * 默认超时时间为20分钟
     */
    public static final int DEFAULT_EXPIRED_TIME = 20 * 60 + 1;

    public static final int EXPIRED_TIME_FIVE_MINUTE = 60 * 5;

    private static List<Pair<String, Integer>> expireTimeFromServerSetting = new ArrayList<Pair<String, Integer>>();
    private static Map<String, Integer> expireTimeMap = new HashMap<String, Integer>();

    public static CacheControl buildPull2RefreshCache() {
        return CacheControl.FORCE_NETWORK;
    }

    public static CacheControl buildDefaultCache() {
        return buildDefaultCache(DEFAULT_EXPIRED_TIME);
    }

    public static CacheControl buildDefaultCache(int expireTimeInSeconds) {
        CacheControl cacheControl = new CacheControl.Builder()
                .maxAge(expireTimeInSeconds, TimeUnit.SECONDS)
                .build();
        return cacheControl;
    }

    public static boolean isExpiredTimeSet(long expireTime) {
        // 如果等于默认时间，则认为外部未设置
        if (expireTime < 0) {
            return false;
        }
        if (expireTime == DEFAULT_EXPIRED_TIME) {
            return false;
        }
        return true;
    }

    public static int getExpiredTime(Request request) {
        if (request == null) {
            return DEFAULT_EXPIRED_TIME;
        }

        String url = request.url().toString();
        if (StringUtils.isBlank(url)) {
            return DEFAULT_EXPIRED_TIME;
        }

        if (expireTimeMap.containsKey(url)) {
            Integer value = expireTimeMap.get(url);
            if (value != null) {
                HttpLog.debug(request, "expiredTime is from expireTimeMap, expiredTime is " + value + "s");
                return toInt(value);
            } else {
                return DEFAULT_EXPIRED_TIME;
            }
        } else {
            if (expireTimeFromServerSetting != null && expireTimeFromServerSetting.size() > 0) {
                for (Pair<String, Integer> pair : expireTimeFromServerSetting) {
                    if (url.contains(pair.first)) {
                        expireTimeMap.put(url, pair.second > 0 ? pair.second : 0);
                        HttpLog.debug(request, "expiredTime is from expireTimeFromServerSetting, expiredTime is "
                                + expireTimeMap.get(url) + "s");
                        return toInt(expireTimeMap.get(url));
                    }
                }
            }
        }
        return DEFAULT_EXPIRED_TIME;
    }

    private static int toInt(Integer value) {
        return value != null ? value : 0;
    }

    public static void setExpireTimeFromServerSetting(List<Pair<String, Integer>> cacheTimeList) {
        if (cacheTimeList != null && cacheTimeList.size() > 0) {
            for (Pair<String, Integer> pair : cacheTimeList) {
                HttpLog.debug("setExpireTimeFromServerSetting, apiKey is " + pair.first + ", value is " + pair.second);
            }
        } else {
            HttpLog.debug("setExpireTimeFromServerSetting, ServerSetting is empty");
        }

        if (cacheTimeList != null) {
            expireTimeFromServerSetting = cacheTimeList;
            expireTimeMap.clear();
        }
    }
}

